package com.itheima.sc.service.pms;

import com.itheima.sc.api.pms.ProductAttributeCategoryApi;
import com.itheima.sc.pojo.pms.ProductAttribute;
import com.itheima.sc.pojo.pms.ProductAttributeCategory;
import com.itheima.sc.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductAttributeCategoryService {

    @DubboReference
    private ProductAttributeCategoryApi attributeCategoryApi;
    @Autowired
    private ProductAttributeService attributeService;

    //添加商品属性分类
    public void create(String name) {
        ProductAttributeCategory attributeCategory = new ProductAttributeCategory();
        attributeCategory.setName(name);
        attributeCategoryApi.save(attributeCategory);
    }

    //删除单个商品属性分类
    public void delete(Integer id) {
        attributeCategoryApi.removeById(id);
        //删除attribute表中对应的数据
        attributeService.deleteByCategoryId(id);
    }

    //分页获取所有商品属性分类
    public PageResult listByPage(Integer pageNum, Integer pageSize) {
        //提取list
        List<ProductAttributeCategory> attributeCategoryList = attributeCategoryApi.listByPage(pageNum,pageSize);

        //查询totalCount
        Integer total = attributeCategoryApi.count();
        Integer totalPage = total/pageNum+1;
        //封装pageResult
        PageResult<ProductAttributeCategory> pageResult = new PageResult<>(pageNum, pageSize, total, totalPage, attributeCategoryList);
        return pageResult;
    }

    //获取所有商品属性分类及其下属性
    public List<ProductAttributeCategory> listWithAttr() {
        List<ProductAttributeCategory> categoryList = attributeCategoryApi.list();
        //封装每个catory里的attrbuteList
        for (ProductAttributeCategory category : categoryList) {
            //获取cid对应的attr集合
           List<ProductAttribute> attributeList = attributeService.listByCategoryId(category.getId());
           category.setProductAttributeList(attributeList);
        }
        return categoryList;
    }

    //修改商品属性分类
    public void updateById(Integer id, String name) {
        ProductAttributeCategory category = attributeCategoryApi.getById(id);
        category.setName(name);
        attributeCategoryApi.updateById(category);
    }

    //获取单个商品属性分类信息
    public ProductAttributeCategory getById(Integer id) {
        ProductAttributeCategory category = attributeCategoryApi.getById(id);
        return category;
    }







}
